package controller.manufacture;

import java.sql.SQLException;

import service.CoffeeService;
import to.JobTO;
import util.CoffeeUtil;

public class Roster extends Thread {
	private static CoffeeService service;

	public Roster(CoffeeService service) {
		this.service = service;
	}

	@Override
	public void run() {
		while (true) {
			try {
				int job = service.selectMachineWaitingJob("로스터");
//				System.out.println("job = " + job);
				while (job > 0) {
					String startTime = CoffeeUtil.getNow();
					// 생두Slio에서 중량 - 쿼리 100kg
					// 로스터 상태값 변경할 쿼리 - 로스팅중
					try {
						service.updateSiloStatus("생두silo", -150);
						CoffeeUtil.sleep(2000);
						String eTime = CoffeeUtil.getNow();
						// 생두 출고 작업 등록
						JobTO jto = new JobTO("생두출고", startTime, eTime);
						jto.setWorkLoad(-150);
						service.insertJobInfo(jto);
						service.updateMachineWaitingJob("로스터", -1);
						service.updateMachineStatus("로스터", "로스팅중");
					} catch (SQLException e) {
						e.printStackTrace();
					}
					String startTime2 = CoffeeUtil.getNow();
					CoffeeUtil.sleep(CoffeeUtil.ROSTIONG_TIME);// 로스팅 시간 10분
					int weightChange = (int) ((Math.random() * 3) + 1);
					int resultWeight = (int) (CoffeeUtil.ROSTION_WEIGHT - CoffeeUtil.ROSTION_WEIGHT
							* (weightChange * 0.01));
					System.out.println(resultWeight);
					String endTime = CoffeeUtil.getNow();
					// 로스터 상태값 변경할 쿼리 - 대기중
					try {
						service.updateMachineStatus("로스터", "대기중");
						// 작업 정보 등록
						JobTO jto = new JobTO("로스팅", startTime2, endTime);
						jto.setWorkLoad(resultWeight);
						service.insertJobInfo(jto);
					} catch (SQLException e) {
						e.printStackTrace();
					}
					
					int bigBeansTotalWeight = 0;
					try {
						bigBeansTotalWeight = service
								.selectBigBeansTotalWeight();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
					cooling();// 쿨링
					// 배전두 Silo에 중량 추가 - resultWeight
					// 서비스단에서 %함량 체크한 후 Silo에 넣는다.
					if (CoffeeUtil.BIG_BEANS_WEIGHT > bigBeansTotalWeight) {// 300kg이
																			// 안될때
						int bigBeans = (int) ((Math.random() * 11) + 20);// 큰 원두
																			// 함량
																			// 10~30%
						try {
							String sTime = CoffeeUtil.getNow();
							CoffeeUtil.sleep(2000);
							service.updatetBigBeansTotalWeight(bigBeans);
							service.updateSiloStatus("배전두silo", resultWeight
									- bigBeans);
							String eTime = CoffeeUtil.getNow();
							// 배전두 silo 입고 작업 등록
							JobTO jto = new JobTO("배전두큰원두입고", sTime, eTime);
							jto.setWorkLoad(bigBeans);
							service.insertJobInfo(jto);
							JobTO jto2 = new JobTO("배전두보통원두입고", sTime, eTime);
							jto2.setWorkLoad(resultWeight - bigBeans);
							service.insertJobInfo(jto2);
						} catch (SQLException e) {
							e.printStackTrace();
						}
					} else {
						// 전부 그냥원두에 추가
						try {
							String sTime = CoffeeUtil.getNow();
							CoffeeUtil.sleep(2000);
							service.updateSiloStatus("배전두silo", resultWeight);
							String eTime = CoffeeUtil.getNow();
							JobTO jto = new JobTO("배전두보통원두입고", sTime, eTime);
							jto.setWorkLoad(resultWeight);
							service.insertJobInfo(jto);
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
					job = service.selectMachineWaitingJob("로스터");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	static public void cooling() {

		// 쿨러 상태 변경 - 쿨링중
		try {
			String startTime = CoffeeUtil.getNow();
			service.updateMachineStatus("쿨러", "쿨링중");
			CoffeeUtil.sleep(CoffeeUtil.COOLING_TIME);// 쿨링 4분
			
			String endTime = CoffeeUtil.getNow();
			JobTO jto = new JobTO("로스터 쿨링", startTime, endTime);
			// 쿨러 상태 변경 - 대기중
			service.insertJobInfo(jto);
			service.updateMachineStatus("쿨러", "대기중");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void clean() {
		// 로스터 상태값 변경 - 청소
		try {
			String startTime = CoffeeUtil.getNow();
			service.updateMachineStatus("로스터", "청소중");
			CoffeeUtil.sleep(CoffeeUtil.CLEAN_TIME);// 청소시간 2분
			service.updateMachineStatus("로스터", "대기중");
			String cleanDate = CoffeeUtil.getNow();
			String endTime = CoffeeUtil.getNow();
			JobTO jto = new JobTO("로스터 청소", startTime, endTime);
			// 쿨러 상태 변경 - 대기중
			service.insertJobInfo(jto);
			service.updateCleanDate("로스터", cleanDate);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
